<?php

/**
 * Gestion des catégories
 */
class Releve extends SimpleContentManager
{
    // Attributs
    const COLLECTION = '9';

    /**
     * Constructeur
     */
    public function __construct()
    {
        $this->_collection = self::COLLECTION;
        parent::__construct();
    }

    public function getByVille($villeID)
    {
        $ShopsList  = BASE::Load(CLASS_SHOP)->findBy('ville', (string) $villeID);
        $releves = array();
        if (is_object($ShopsList)) {
            foreach ($ShopsList as $s) {
                $idMag = (string) $s['_id'];
                $rel = BASE::Load(CLASS_RELEVE)->findBy('magasin', $idMag);
                if (is_object($rel)) {
                    foreach ($rel as $r) {
                        $releves[]  = BASE::Load(CLASS_RELEVE)->get((string)$r['_id'], true);
                    }
                }
            }
        }
        return $releves;
    }

    public function getMediane($product_ID)
    {
        $releves = $this->findBy('product', (string) $product_ID, true);
        $price = array();
        $pMediane = null;
        if (is_array($releves)) {
            foreach ($releves as $i => $r) {
                if (isset($releves[$i]['n_total']) && $releves[$i]['n_nb'] > 0) {
                    $releves[$i]['note'] = round($releves[$i]['n_total'] / $releves[$i]['n_nb']);
                } else {
                    $releves[$i]['note'] = 0;
                }
                $price[] = $r['prix'];
            }
            $pMediane = Tools::mediane($price);
        }
        return array('mediane' => $pMediane, 'price' => $price,'releves' => $releves);
    }

    public function getLast($limit = 10)
    {
        $filter = array(
            "collection" => (string)$this->_collection,
        );

        $tmp = $this->_bdd
                ->find($filter)
                ->sort( array('$natural' => -1 ) )
                ->limit($limit);

        foreach ($tmp as $d) {
            $d['product'] = Base::Load(CLASS_PRODUCT)->get($d['product']);
            $a[] = $d;
        }

        return $a;
    }

    public function getVilleLessCost($limit = 10)
    {
        $releves = $this->getAll();

        $notation = array();
        $countPx = array();
        foreach ($releves as $releve) {
            if (isset($notation[(string)$releve['magasin']])) {
                $notation[(string)$releve['magasin']] += $releve['prix'];
                $countPx[(string)$releve['magasin']]++;
            } else {
                $notation[(string)$releve['magasin']] = $releve['prix'];
                $countPx[(string)$releve['magasin']] = 1;
            }
        }

        $villes = array();
        foreach ($notation as $magasinID => $mt) {
            $mag = Base::Load(CLASS_SHOP)->get($magasinID);
            $vi = Base::Load(CLASS_LOC)->getVille($mag['ville']);
            $villes[$vi[0]]['name'] = $vi[2];
            $villes[$vi[0]]['prix'] = round($mt / $countPx[$magasinID]);
            $villes[$vi[0]]['id'] = $vi[0];
        }
        foreach ($villes as $v) {
            $tmp[$v['prix']] = $v;
        }

        ksort($tmp);

        $return = array();
        foreach ($tmp as $k => $tmp2) {
            if ($k < $limit) {
                $return[] = $tmp2;
            }
        }

        return $return;
    }

    /**
     * Classement des villes les plus couteuses
     * @param int $limit
     * @return array
     */
    public function getVilleMostCost($limit = 10)
    {
        $releves = $this->getAll();

        $notation = array();
        $countPx = array();
        foreach ($releves as $releve) {
            if (isset($notation[(string)$releve['magasin']])) {
                $notation[(string)$releve['magasin']] += $releve['prix'];
                $countPx[(string)$releve['magasin']]++;
            } else {
                $notation[(string)$releve['magasin']] = $releve['prix'];
                $countPx[(string)$releve['magasin']] = 1;
            }
        }

        $villes = array();
        foreach ($notation as $magasinID => $mt) {
            $mag = Base::Load(CLASS_SHOP)->get($magasinID);
            $vi = Base::Load(CLASS_LOC)->getVille($mag['ville']);
            $villes[$vi[0]]['name'] = $vi[2];
            $villes[$vi[0]]['prix'] = round($mt / $countPx[$magasinID]);
            $villes[$vi[0]]['id'] = $vi[0];
        }
        foreach ($villes as $v) {
            $tmp[$v['prix']] = $v;
        }
        ksort($tmp);
        $tmp = array_reverse($tmp);

        $return = array();
        foreach ($tmp as $k => $tmp2) {
            if ($k < $limit) {
                $return[] = $tmp2;
            }
        }

        return $return;
    }
}


/* -- Fin du fichier -- */